﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Sql;

public partial class Boss_EditStuff : System.Web.UI.Page
{
    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    string Sno = Request.QueryString["Sno"];
    //    Response.Write(Sno);
    //}

    //string username;
    //string password;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string Sno = Request.QueryString["Sno"];
            fullFillData(Sno);
        }

    }

    private void fullFillData(string Sno)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrManager"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Manager_Staff where Sno = @Sno";
                SqlParameter[] parameters = { new SqlParameter("@Sno", Sno) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    dr.Read();

                    SnoTex.Text = dr["Sno"].ToString();
                    NameTex.Text = dr["Sname"].ToString();
                    SduteTex.Text = dr["Sdute"].ToString();
                    if (Convert.ToBoolean(dr["Ssex"]) == true)
                        SexRadioList.SelectedValue = "1";
                    else
                        SexRadioList.SelectedValue = "0";
                    PhoneTex.Text = long.Parse(dr["Sphone"].ToString()).ToString();
                    BirthTex.Text = dr["Sbirth"].ToString();
                    dr.Close();
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }


    }
    protected void ResetBut_Click(object sender, EventArgs e)
    {
        string Sno = Request.QueryString["Sno"];
        fullFillData(Sno);
    }
    protected void UpdateBut_Click(object sender, EventArgs e)
    {
        string Sno = Request.QueryString["Sno"];
        saveData(Sno);
    }

    private void saveData(string Sno)
    {

        bool sex = true;
        if (SexRadioList.SelectedValue == "1")
            sex = true;
        else
            sex = false;
        string tel = PhoneTex.Text;
        string birth = BirthTex.Text;
        string name = NameTex.Text;
        string Sdute = SduteTex.Text;
        string Psw = PswTex.Text;
        string RePsw = RePswTex.Text;

        if (checkData(name, tel, birth, Sdute, Psw, RePsw) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrManager"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "update Manager_Staff set Sname = @name  , Ssex= @sex,  Sphone = @tel,Sdute = @Sdute  where Sno = @Sno";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@name", name),
                                                new SqlParameter("@sex", sex),
                                                new SqlParameter("@tel", tel),                                                
                                                new SqlParameter("@Sno", Sno),
                                                new SqlParameter("@Sdute", Sdute)
                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private bool checkData(string name, string tel, string birth,string Sdute,string Psw,string RePsw)
    {
        bool ans = true;
        if (!(name.Length > 0))
            ans = false;
        else if (tel.Length > 11)
            ans = false;
        else if (birth.Length > 100)
            ans = false;
        else if (!(Sdute == "店长" || Sdute == "送餐员" || Sdute == "服务员"))
            ans = false;
        else if (Psw != RePsw)
            ans = false;
        return ans;
    }

    protected void DelBut_Click(object sender, EventArgs e)
    {
        bool flag = false;
        //查看订单是否有该员工派送中的，有的话拒绝
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        string Sno = Request.QueryString["Sno"];
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Boss_Order  where Sno = @Sno and Ostate = '正在派送'";
                SqlParameter[] parameters = { new SqlParameter("@Sno", Sno) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr == null || dr.Read() == false)
                    {
                        flag = true;
                    }
                    dr.Close();
                }
                if(flag == false)
                {//正在派送，无法删除
                    Response.Write(@"<script>alert('该员工正在派送，无法删除！');</script>");
                }
                else
                {
                    sqlString = "delete from Boss_Staff where Sno = @Sno";
                    SqlParameter[] parametersnew = {                                             
                                                new SqlParameter("@Sno", Sno)
                                            };
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sqlString;
                        cmd.Parameters.AddRange(parametersnew);
                        int ret = cmd.ExecuteNonQuery();
                        if (ret > 0)
                        {
                            Response.Write(@"<script>alert('删除成功');</script>");
                            Response.Redirect("./StaffList.aspx");
                        }
                        else
                        {
                            Response.Write(@"<script>alert('修改失败');</script>");
                        }
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void InsBut_Click(object sender, EventArgs e)
    {
        bool Ssex = true;
        if (SexRadioList.SelectedValue == "1")
            Ssex = true;
        else
            Ssex = false;
        string Sphone = PhoneTex.Text;
        string Sbirth = BirthTex.Text;
        string Sname = NameTex.Text;
        string Sdute = SduteTex.Text;
        string Sno = SnoTex.Text;
        string SPsw = PswTex.Text;
        string RePsw = RePswTex.Text;
        if (checkData(Sname, Sphone, Sbirth, Sdute, SPsw, RePsw) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "insert Boss_Staff (Sno,Sname,Sphone,Sdute,Ssex,Rno,Sbirth,Spswd)values (@Sno,@Sname,@Sphone,@Sdute,@Ssex,@Rno,@Sbirth,@SPsw)";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@Sno", Sno),
                                                new SqlParameter("@Sname", Sname),
                                                new SqlParameter("@Sphone", Sphone),                                                
                                                new SqlParameter("@Sdute", Sdute),
                                                new SqlParameter("@Ssex", Ssex),
                                                new SqlParameter("@Rno", "001"),
                                                new SqlParameter("@Sbirth",Sbirth),
                                                new SqlParameter("@SPsw", SPsw),

                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}